package com.zjc.yohoshop.mvp.persenter;

import android.util.Log;

import com.zjc.lib_core.mvp.presenter.BasePresenter;
import com.zjc.yohoshop.bean.BaseEntity;
import com.zjc.yohoshop.bean.CategoryLeftEntity;
import com.zjc.yohoshop.bean.CategoryRightEntity;
import com.zjc.yohoshop.mvp.contract.CategoryFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CategoryFragmentPresenter extends BasePresenter<CategoryFragmentContract.CategoryFragmentIView, CategoryFragmentContract.CategoryFragmentIModel> {


    @Inject
    public CategoryFragmentPresenter(CategoryFragmentContract.CategoryFragmentIView categoryFragmentIView, CategoryFragmentContract.CategoryFragmentIModel categoryFragmentIModel) {
        super(categoryFragmentIView, categoryFragmentIModel);
    }


    //发起全部网络请求
    public void requestAll(){
        Map<String, String> map = new HashMap<>();
        JSONObject left = new JSONObject();
        try {
            left.put("menuid", "" + 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        map.put("left", left.toString());
        JSONObject right = new JSONObject();
        try {
            right.put("categoryid", "" + 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        map.put("right", right.toString());
        m.requestCategoryAll(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(BaseEntity baseEntity) {
                        if (baseEntity instanceof CategoryLeftEntity){
                            v.categoryLeftSuccess((CategoryLeftEntity) baseEntity);
                        }else if (baseEntity instanceof CategoryRightEntity){
                            v.categoryRightSuccess((CategoryRightEntity) baseEntity);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("ZJC", "CategoryFragmentPresenter -> onError: -> " + e.toString() );
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void requestRight(String categoryid){
        JSONObject right = new JSONObject();
        try {
            right.put("categoryid", "" + 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.reqyestCategoryRight(right.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CategoryRightEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(CategoryRightEntity entity) {
                        v.categoryRightSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("ZJC", "onError: " + e.toString() );
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
